package org.rzp.ideaplug.push.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.rzp.ideaplug.PushResponse;
import org.rzp.ideaplug.TableResponse;
import org.rzp.ideaplug.push.entity.StatisticInfo;
import org.rzp.ideaplug.push.service.IStatisticInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author rzp
 * @since 2020-05-28
 */
@Controller
public class StatisticInfoController {

    @Autowired
    private IStatisticInfoService statisticInfoService;


    @PostMapping("push")
    @ResponseBody
    public PushResponse pushStatistic(StatisticInfo statisticInfo) {
        statisticInfo.setDate(LocalDateTime.now());
        statisticInfoService.save(statisticInfo);
        PushResponse pushResponse = new PushResponse();
        pushResponse.setCode(200);
        pushResponse.setMessage("OK");
        return pushResponse;
    }

    @GetMapping("/")
    public String toPushInfoView() {
        return "push";
    }


    @PostMapping("page")
    @ResponseBody
    public TableResponse selectPage(int page, int limit, StatisticInfo statisticInfo) {
        Page<StatisticInfo> statisticInfoPage = new Page<>(page, limit);
        QueryWrapper<StatisticInfo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(statisticInfo.getProject())) {
            wrapper.like("f_project", statisticInfo.getProject());
        }
        if (StringUtils.isNotBlank(statisticInfo.getUser())) {
            wrapper.like("f_user", statisticInfo.getUser());
        }
        wrapper.orderByDesc("f_date").orderByDesc("f_id");
        statisticInfoPage = statisticInfoService.page(statisticInfoPage, wrapper);
        return TableResponse.convert(statisticInfoPage);
    }
}

